package snsdbook.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.SQLUtil;
import util.Util;

/**
 * Servlet implementation class UsernameServlet
 */
public class UsernameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UsernameServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  String type = request.getParameter("type");
    String username = request.getParameter("username");
    String query = null;
    boolean is_query = false;
    if (type.equals("getavatar")) {
      query = "SELECT avatar FROM users WHERE username='" + username + "'";
      is_query = true;
    } else if (type.equals("getpassword")) {
      query = "SELECT password FROM users WHERE username='" + username + "'";
      is_query = true;
    }
    if (is_query) {
      String[] result = SQLUtil.Query(query);
      StringBuilder output = new StringBuilder();
      for (String str : result) {
        if (output.length() > 0) {
          output.append(",");
        }
        output.append(str);
      }
      
      Util.PrintAndClose(response, output.toString());
      return;
    }
	}

}
